package com.hzncc.scaffold.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.hzncc.scaffold.entity.Bo.QuestionBankCourseBo;
import com.hzncc.scaffold.entity.Bo.QuestionLibListBo;
import com.hzncc.scaffold.entity.Other.Questionlib;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzncc.scaffold.entity.Vo.QuestionBank;
import com.hzncc.scaffold.entity.Vo.QuestionVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 题库表 Mapper 接口
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Component
public interface QuestionlibMapper extends BaseMapper<Questionlib> {

//    @Select("SELECT\n" +
//            "\t`questionLib`.*,\n" +
//            "\t`operator`.`name` AS `operatorName`,\n" +
//            "\t`majors`.`id` AS `majorsId`,\n" +
//            "\t`majors`.`name` AS `majorsName`,\n" +
//            "\t`majors`.`pid` AS `majorsPid`,\n" +
//            "\t`questions`.`id` AS `questionsId`,\n" +
//            "\t`questions`.`type` AS `questionsType` \n" +
//            "FROM\n" +
//            "\t(\n" +
//            "\tSELECT\n" +
//            "\t\t`questionLib`.`id`,\n" +
//            "\t\t`questionLib`.`title`,\n" +
//            "\t\t`questionLib`.`createTime`,\n" +
//            "\t\t`questionLib`.`operatorId` \n" +
//            "\tFROM\n" +
//            "\t\t`questionLib` AS `questionLib` \n" +
//            "\tWHERE\n" +
//            "\t\t`questionLib`.`isDelete` = 0 \n" +
//            "\tORDER BY\n" +
//            "\t\t`questionLib`.`createTime` DESC \n" +
//            "\t) AS `questionLib`\n" +
//            "\tLEFT OUTER JOIN `user` AS `operator` ON `questionLib`.`operatorId` = `operator`.`id`\n" +
//            "\tLEFT OUTER JOIN ( `QlibMajor` AS `majors->QlibMajor`\n" +
//            "\tINNER JOIN `major` AS `majors` ON `majors`.`id` = `majors->QlibMajor`.`majorId` ) ON `questionLib`.`id` = `majors->QlibMajor`.`questionLibId`\n" +
//            "\tLEFT OUTER JOIN `question` AS `questions` ON `questionLib`.`id` = `questions`.`libId` \n ${ew.customSqlSegment}" +
//            "\tORDER BY\n" +
//            "\t`questionLib`.`createTime` DESC,\n" +
//            "\t`questions`.`type` ASC")
//    public List<QuestionBank> list(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\t`questionlib`.*,\n" +
            "\t`operator`.`name` AS `operatorName`,\n" +
            "\t`questions`.`id` AS `questionsId`,\n" +
            "\t`questions`.`type` AS `questionsType` \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\t`questionlib`.`id`,\n" +
            "\t\t`questionlib`.`title`,\n" +
            "\t\t`questionlib`.`createTime`,\n" +
            "\t\t`questionlib`.`operatorId` \n" +
            "\tFROM\n" +
            "\t\t`questionlib` AS `questionlib` \n" +
            "\tWHERE\n" +
            "\t\t`questionlib`.`isDelete` = 0 \n" +
            "\tORDER BY\n" +
            "\t\t`questionlib`.`createTime` DESC \n" +
            "\t) AS `questionlib`\n" +
            "\tLEFT OUTER JOIN `user` AS `operator` ON `questionlib`.`operatorId` = `operator`.`id`\n" +
            "\tLEFT OUTER JOIN `question` AS `questions` ON `questionlib`.`id` = `questions`.`libId` \n ${ew.customSqlSegment}" +
            "ORDER BY\n" +
            "\t`questionlib`.`createTime` DESC,\n" +
            "\t`questions`.`type` ASC")
    public List<QuestionBank> list(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\t`question`.`id`,\n" +
            "\t`question`.`title`,\n" +
            "\t`question`.`option`,\n" +
            "\t`question`.`type`,\n" +
            "\t`question`.`answer`,\n" +
            "\t`question`.`analyse`,\n" +
            "\t`questionlib`.`id` AS `questionlib`,\n" +
            "\t`questionlib`.`title` AS `questionLibTitle` \n" +
            "FROM\n" +
            "\t`question` AS `question`\n" +
            "\tLEFT OUTER JOIN `questionlib` AS `questionlib` ON `question`.`libId` = `questionlib`.`id` \n ${ew.customSqlSegment}" +
            "ORDER BY\n" +
            "\t`question`.`type` ASC")
    public List<QuestionVo> detail(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT questionlib.title FROM `questionlib`")
    public List<String> getTitleList();


//    @Select("SELECT\n" +
//            "\tquestionlib.*\n" +
//            "FROM\n" +
//            "\t`questionlib`\n" +
//            "LEFT JOIN course ON course.title = questionlib.title ${ew.customSqlSegment}")
//    public List<Questionlib> questionLibList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\tquestionlib.id AS questionLibId,\n" +
            "\t`course`.*,\n" +
            "\t`chapters`.`id` AS `chaptersId`,\n" +
            "\t`chapters->tasks`.`examStart`,\n" +
            "\t`chapters->tasks`.`examEnd`,\n" +
            "\tpa.examTime,\n" +
            "\tpa.totalScore \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\t`course`.`id`,\n" +
            "\t\t`course`.`title`,\n" +
            "\t\t`course`.`createTime`,\n" +
            "\t\t`course`.`cover`,\n" +
            "\t\t`course`.`teacher`,\n" +
            "\t\t`course`.`joinedNum`,\n" +
            "\t\t`course`.`isFree`,\n" +
            "\t\t`course`.`pubTime`,\n" +
            "\t\tcourse.isQuestionLibExercise \n" +
            "\tFROM\n" +
            "\t\t`course` AS `course` \n" +
            "\tWHERE\n" +
            "\t\tcourse.STATUS = 1 \n" +
            "\t\tAND course.isDelete = 0 \n" +
            "\tORDER BY\n" +
            "\t\t`course`.`pubTime` DESC \n" +
            "\t) AS `course`\n" +
            "\tLEFT OUTER JOIN ( `coursemajor` AS `majors->coursemajor` INNER JOIN `major` AS `majors` ON `majors`.`id` = `majors->coursemajor`.`majorId` ) ON `course`.`id` = `majors->coursemajor`.`courseId`\n" +
            "\tLEFT OUTER JOIN `chapter` AS `chapters` ON `course`.`id` = `chapters`.`courseId`\n" +
            "\tLEFT OUTER JOIN `task` AS `chapters->tasks` ON `chapters`.`id` = `chapters->tasks`.`chapterId`\n" +
            "\tLEFT JOIN questionlib ON questionlib.title = course.title\n" +
            "\tLEFT JOIN paper pa ON pa.id = `chapters->tasks`.`paperId`\n " +
            "\t${ew.customSqlSegment}")
    public List<QuestionBankCourseBo> questionLibList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\tCOUNT( DISTINCT id ) \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\tquestionlib.id AS questionLibId,\n" +
            "\t\t`course`.*,\n" +
            "\t\t`chapters`.`id` AS `chaptersId` \n" +
            "\tFROM\n" +
            "\t\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\t`course`.`id`,\n" +
            "\t\t\t`course`.`title`,\n" +
            "\t\t\t`course`.`cover`,\n" +
            "\t\t\t`course`.`teacher`,\n" +
            "\t\t\t`course`.`joinedNum`,\n" +
            "\t\t\t`course`.`isFree`,\n" +
            "\t\t\t`course`.`pubTime`,\n" +
            "\t\t\tcourse.isQuestionLibExercise \n" +
            "\t\tFROM\n" +
            "\t\t\t`course` AS `course` \n" +
            "\t\tWHERE\n" +
            "\t\t\tcourse.STATUS = 1 \n" +
            "\t\t\tAND course.isDelete = 0 \n" +
            "\t\tORDER BY\n" +
            "\t\t\t`course`.`pubTime` DESC \n" +
            "\t\t) AS `course`\n" +
            "\t\tLEFT OUTER JOIN ( `coursemajor` AS `majors->coursemajor` INNER JOIN `major` AS `majors` ON `majors`.`id` = `majors->coursemajor`.`majorId` ) ON `course`.`id` = `majors->coursemajor`.`courseId`\n" +
            "\t\tLEFT OUTER JOIN `chapter` AS `chapters` ON `course`.`id` = `chapters`.`courseId`\n" +
            "\t\tLEFT OUTER JOIN `task` AS `chapters->tasks` ON `chapters`.`id` = `chapters->tasks`.`chapterId`\n" +
            "\t\tLEFT JOIN questionlib ON questionlib.title = course.title \n ${ew.customSqlSegment}" +
            "\tORDER BY\n" +
            "\t`course`.`pubTime` DESC \n " +
            "\t) AS total")
    public Integer selectTotalPlus(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\tquestionlib.* ,\n" +
            "\tmajor.id AS majorId,\n" +
            "\tmajor.`name` AS majorName,\n" +
            "\tmajor.pid AS majorPid\n" +
            "FROM\n" +
            "\t`questionlib`\n" +
            "LEFT JOIN qlibmajor ON qlibmajor.questionLibId = questionlib.id\n" +
            "LEFT JOIN major ON qlibmajor.majorId = major.id ${ew.customSqlSegment}")
    public List<Questionlib> listQuestionLib(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\tCOUNT(\n" +
            "\tDISTINCT ( questionlib.id )) \n" +
            "FROM\n" +
            "\t`questionlib`\n" +
            "\tLEFT JOIN qlibmajor ON qlibmajor.questionLibId = questionlib.id\n" +
            "\tLEFT JOIN major ON qlibmajor.majorId = major.id ${ew.customSqlSegment}")
    public Integer selectTotal(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\t`questionlib`.`id`,\n" +
            "\t`questionlib`.`title`,\n" +
            "\t`majors`.`id` AS `majorsId`,\n" +
            "\t`majors`.`name` AS `majorsName`\n" +
            "FROM\n" +
            "\t`questionlib` AS `questionlib`\n" +
            "\tINNER JOIN ( `qlibmajor` AS `majors->qlibmajor` INNER JOIN `major` AS `majors` ON `majors`.`id` = `majors->qlibmajor`.`majorId` ) ON `questionlib`.`id` = `majors->qlibmajor`.`questionLibId` ${ew.customSqlSegment}")
    public List<QuestionLibListBo> questionLibListBo(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);
}
